import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';

import { orderStateArrs } from '../../love-recyc-manage.pipe';
import { CommonService } from '../../../../services-common/common.service';
import { LuckDrawManageService } from '../luck-draw-manage.service';

@Component({
  selector: 'app-love-luckdraw-record',
  templateUrl: './love-luckdraw-record.component.html',
  styleUrls: ['./love-luckdraw-record.component.scss']
})
export class LoveLuckdrawRecordComponent implements OnInit {

  routeParams: any = {};
  searchOptions = [
    { type: 1, labelText: '活动名称', valueKey: 'name', value: '' },
    { type: 1, labelText: '订单编号', valueKey: 'orderNum', value: '' },
    {
      type: 2,
      labelText: '订单状态',
      valueKey: 'orderStatus',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'id', label: 'label' },
      selectOptions: [...orderStateArrs],
    },
    { type: 3, labelText: '中奖时间', valueKey: ['dayBeginTime', 'dayEndTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  exportLoading = false;

  constructor(
    private msg: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private modal: NzModalService,
    private commonService: CommonService,
    private services: LuckDrawManageService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};

      if (res.activityName) {
        this.searchOptions[0].value = res.activityName;
      }
    })
  }

  ngOnInit(): void {
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }

    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getLoveLuckDrawRecordData(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    }
  }

  queryData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData(params: any) {
    this.searchParams = params;
    if (this.routeParams.activityName) {
      this.searchOptions[0].value = this.routeParams.activityName;
      this.searchParams.name = this.routeParams.activityName;
    }

    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  exportTableData() {
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '即将导出符合本页面筛选条件的数据，为了导出速度请尽可能的控制时间范围。确认导出？',
      nzOkText: '确认',
      nzOkType: 'primary',
      nzCancelText: '取消',
      nzOkDanger: true,
      nzOnOk: async () => {
        let params = {
          ...this.searchParams
        }
        this.exportLoading = true;
        const res = await this.services.exportLoveLuckDrawRecordData(params);
        this.exportLoading = false;
    
        if (res.type === 'application/octet-stream') {
          this.commonService.downLoadBlobFile(res, '回收下单抽奖记录');
        } else {
          this.msg.error('导出失败');
        }
      }
    });
  }
}
